<?php
  // 新增图书后端保存程序
  include_once('../conn.php');
  $book_name = $_POST['book_name'];
  $book_category = $_POST['book_category'];
  $book_position = $_POST['book_position'];
  $book_count = $_POST['book_count'];
  
  $sql = "select * from book where book_name = '$book_name'";
 
  $result = mysqli_query($conn, $sql);
  if($result) {
    alert('该图书已存在', 'book_new.php');
    exit();
  }
  
  if(strlen($book_name) < 1) {
    alert('图书名称不能小于1位', 'book_save.php');
    exit();
  }

  // 新增一条图书信息
  $sql = "insert into book (book_name, book_category, book_position, book_count) values ('$book_name', '$book_category', '$book_position', $book_count)";
  // echo $sql;
  $result = mysqli_query($conn, $sql);
  if($result) {
    alert('添加成功', 'book_list.php');
  } else {
    alert('添加失败', 'book_save.php');
  }

  mysqli_close($conn);
?>